package com.sw.controller;

import com.github.pagehelper.PageHelper;
import com.google.gson.Gson;
import com.sw.domain.ActiveEnrollListPage;
import com.sw.mapper.ActiveEnrollMapper;
import com.sw.model.ActiveEnroll;
import com.sw.model.ActiveEnrollExample;
import com.sw.model.Originator;
import com.sw.service.ActiveEnrollService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.sw.exception.InvalidArgumentException;

import java.util.List;

/**
 * Created by Administrator on 2017/7/12.
 */
@RestController
@RequestMapping(value = "active_enroll")
public class ActiveEnrollController {
    @Autowired
    private ActiveEnrollService activeEnrollService;
    @Autowired
    private ActiveEnrollMapper activeEnrollMapper;
    protected Logger log= LoggerFactory.getLogger(getClass());
    Gson gson = new Gson();

    /**创建活动报名条目
     * @param activeEnroll
     * @return
     */
    @RequestMapping(value = "",method = RequestMethod.POST)
    public ActiveEnroll insert(@RequestBody ActiveEnroll activeEnroll){
        ActiveEnrollExample example=new ActiveEnrollExample();
        example.createCriteria().andActiveIdEqualTo(activeEnroll.getActiveId()).andUserIdEqualTo(activeEnroll.getUserId());
        if (activeEnrollMapper.selectByExample(example).size()!=0){
            throw new InvalidArgumentException("该用户已参加该活动请勿重复报名");
        }else {
            activeEnroll.setEnrolmentState("0");
            activeEnrollMapper.insertSelective(activeEnroll);
            return activeEnrollMapper.selectByExample(example).get(0);
        }
    }

    /**根据活动ID和用户ID删除接口
     * @param activeId
     * @param userId
     */
    @RequestMapping(value = "{activeId}/{userId}",method = RequestMethod.DELETE)
    public ActiveEnroll delete(@PathVariable int activeId,@PathVariable int userId){
        ActiveEnrollExample example = new ActiveEnrollExample();
        example.createCriteria().andActiveIdEqualTo(activeId).andUserIdEqualTo(userId);
        if (activeEnrollMapper.selectByExample(example).size()==0){
            throw new InvalidArgumentException("该记录不存在，请核对activeId和uerId是否正确");
        }else {
            ActiveEnroll activeEnroll=new ActiveEnroll();
            activeEnroll.setEnrolmentState("4");
            activeEnrollMapper.updateByExampleSelective(activeEnroll,example);
            return activeEnrollMapper.selectByExample(example).get(0);
        }
    }

    /**根据用户ID和活动ID修改条目接口
     * @param activeId
     * @param userId
     * @param activeEnroll
     * @return
     */
    @RequestMapping(value = "{activeId}/{userId}",method = {RequestMethod.POST,RequestMethod.PUT})
    public ActiveEnroll update(@PathVariable int activeId,@PathVariable int userId,@RequestBody ActiveEnroll activeEnroll){
        ActiveEnrollExample example=new ActiveEnrollExample();
        example.createCriteria().andActiveIdEqualTo(activeId).andUserIdEqualTo(userId);
        if (activeEnrollMapper.selectByExample(example).size()==0){
            throw new InvalidArgumentException("该记录不存在，请核对activeId和uerId是否正确");
        }else {
            activeEnroll.setActiveId(activeId);
            activeEnroll.setUserId(userId);
            activeEnrollMapper.updateByExampleSelective(activeEnroll,example);
            return activeEnrollMapper.selectByExample(example).get(0);
        }
    }

    /**根据用户ID和活动ID获取条目接口
     * @param activeId
     * @param userId
     * @return
     */
    @RequestMapping(value = "{activeId}/{userId}",method = RequestMethod.GET)
    public ActiveEnroll activeEnroll(@PathVariable int activeId,@PathVariable int userId){
        ActiveEnrollExample example=new ActiveEnrollExample();
        example.createCriteria().andActiveIdEqualTo(activeId).andUserIdEqualTo(userId);
        List<ActiveEnroll>activeEnrollList=activeEnrollMapper.selectByExample(example);
        if (activeEnrollList.size()==0){
            throw new InvalidArgumentException("该记录不存在，请核对activeId和uerId是否正确");
        }else {
            return activeEnrollList.get(0);
        }
    }

    /**获取所有活动报名信息接口
     */
    @RequestMapping(value = "",method = RequestMethod.GET)
    public ActiveEnrollListPage getActiveEnrollList(@RequestParam(required = false) Integer pageNum){
        ActiveEnrollExample example=new ActiveEnrollExample();
        // 查询数据
        if (pageNum!=null){
            PageHelper.startPage(pageNum, 15);
        }
        List<ActiveEnroll>activeEnrollList=activeEnrollMapper.selectByExample(example);
        return activeEnrollService.setActiveEnrollListPage(activeEnrollList);
    }
    /**根据活动ID获取活动报名列表(可根据活动状态筛选)
     * @param activeId
     * @param state
     * @return
     */
    @RequestMapping(value = "ByActiveId/{activeId}",method = RequestMethod.GET)
    public ActiveEnrollListPage queryByActiveId(@PathVariable int activeId,@RequestParam(required = false) Integer pageNum,@RequestParam(required = false) String state){
        log.debug("123state={}",state);
        ActiveEnrollExample example= new ActiveEnrollExample();
        log.debug("example={}",gson.toJson(example));
        ActiveEnrollExample.Criteria criteria=example.createCriteria();
        criteria.andActiveIdEqualTo(activeId);
        log.debug("example={}",gson.toJson(example));
        if(state!=null){
            log.debug("127state={}",state);
            criteria.andEnrolmentStateEqualTo(state);
            log.debug("example={}",gson.toJson(example));
        }
        // 查询数据
        if (pageNum!=null){
            PageHelper.startPage(pageNum, 15);
        }
        log.debug("example={}",gson.toJson(example));
        List<ActiveEnroll>activeEnrollList=activeEnrollMapper.selectByExample(example);
        return activeEnrollService.setActiveEnrollListPage(activeEnrollList);
    }

    /**根据用户ID获取活动报名列表
     * @param userId
     * @return
     */
    @RequestMapping(value = "ByUserId/{userId}",method = RequestMethod.GET)
    public ActiveEnrollListPage queryByUserId(@PathVariable int userId,@RequestParam(required = false) Integer pageNum){
        ActiveEnrollExample example=new ActiveEnrollExample();
        example.createCriteria().andUserIdEqualTo(userId);
        // 查询数据
        if (pageNum!=null){
            PageHelper.startPage(pageNum, 15);
        }
        List<ActiveEnroll>activeEnrollList=activeEnrollMapper.selectByExample(example);
        return activeEnrollService.setActiveEnrollListPage(activeEnrollList);
    }
}
